<?php

if (!defined('BASEPATH'))
     exit('No direct script access allowed');

class Registrasi extends CI_Controller {

     function __construct() {
          parent:: __construct();
     }

     //deklarasi halaman registrasi
     function reg() {
          $span = "span12";
          $nama_user = array('value' => $this->input->post('nama_user'), 'name' => 'nama_user', 'id' => '', 'placeholder' => 'Nama Lengkap...', 'class' => $span,
              'rel' => 'popover', 'data-content' => 'Silakan isikan nama lengkap anda, meliputi nama depan hingga nama belakang.<br><u>wajib diisi</u> <br /><br /><strong>Contoh:</strong> Susilo Bambang Yudoyono', 'data-original-title' => 'Nama Lengkap');

          $username_user = array('value' => $this->input->post('username_user'), 'name' => 'username_user', 'class' => $span, 'placeholder' => 'Username...',
              'rel' => 'popover', 'data-content' => 'Isikan username anda. Username nantinya berfungsi untuk login anda. Username anda harus unique.<br><u>wajib diisi</u> <br /><br /><strong>Contoh:</strong> sby2012', 'data-original-title' => 'Username');

          $password_user = array('name' => 'password_user', 'placeholder' => 'Password...', 'class' => $span,
              'rel' => 'popover', 'data-content' => 'Silakan mengisi password sebagai kunci anda untuk login ke forum. <br><u>wajib diisi</u> <br /><br /><strong>Contoh: </strong>1234sby@5678', 'data-original-title' => 'Password');

          $re_password_user = array('name' => 're_password_user', 'placeholder' => 'Konfirmasi Password...', 'class' => $span,
              'rel' => 'popover', 'data-content' => 'Masukkan konfirmasi password dengan mengetikkan karakter yang sama dengan pengisian kolom password di atas. Hal ini berfungsi sebagai validasi kebenaran password anda. <br><u>wajib diisi</u> <br /><br />', 'data-original-title' => 'Konfirmasi Password');
          //ingat...ada id_rule. setting id_rule default = 1 (pengguna biasa)
          $tempat_lahir_user = array('value' => $this->input->post('tempat_lahir_user'), 'name' => 'tempat_lahir_user', 'placeholder' => 'Tempat Lahir..', 'class' => $span,
              'rel' => 'popover', 'data-content' => 'Isikan Tempat Kelahiran Anda. <br><u>wajib diisi</u> <br /><br /><strong>Contoh: </strong>Jakarta', 'data-original-title' => 'Tempat Lahir');

          $tanggal_lahir_user = array('value' => $this->input->post('tanggal_lahir_user'), 'name' => 'tanggal_lahir_user', 'id' => 'tanggal', 'placeholder' => 'Tanggal Lahir...', 'class' => $span,
              'rel' => 'popover', 'data-content' => 'Masukkan tanggal lahir anda di sini. Anda tinggal menekan kursor anda pada kolom tanggal lahir, maka halaman akan muncul format kalendar<br><u>wajib diisi<u/> <br /><br />', 'data-original-title' => 'Tanggal Lahir');

          $alamat_user = array('value' => $this->input->post('alamat_user'), 'name' => 'alamat_user', 'placeholder' => 'Alamat. Halaman ini tidak wajib diisi. Contoh: jln. Ki Hajar Dewantara No.08 Padang', 'class' => $span);

          $no_ktp_user = array('value' => $this->input->post('no_ktp_user'), 'name' => 'no_ktp_user', 'placeholder' => 'Nomor KTP...', 'class' => $span,
              'rel' => 'popover', 'data-content' => 'Masukkan nomor KTP anda. Hal ini berfungsi untuk validasi pasien/orang tua pasien dan identitasnya dapat dipercaya. <br><u>wajib diisi</u> <br /><br /><strong>Contoh: </strong>3505142201900001', 'data-original-title' => 'Nomor KTP');

          $set_form_registrasi = array('nama_user' => $nama_user,
              'username_user' => $username_user,
              'password_user' => $password_user,
              're_password_user' => $re_password_user,
              'tempat_lahir_user' => $tempat_lahir_user,
              'tanggal_lahir_user' => $tanggal_lahir_user,
              'alamat_user' => $alamat_user,
              'no_ktp_user' => $no_ktp_user
          );

          return $set_form_registrasi;
     }

     function password_check($srt) {
          $password_user = $this->input->post('password_user');
          if ($password_user != $srt) {//false
               $this->form_validation->set_message('password_check', '%s tidak sama dengan input password sebelumnya');
               return false;
          } else {
               return true;
          }
     }

          function sign() {
          $username = array('name' => 'username_user', 'placeholder' => 'Username');
          $password = array('name' => 'password_user', 'Placeholder' => 'Password');
          $sign = array('username_user' => $username, 'password_user' => $password);
          return $sign;
     }
     
     public function login() {
          $this->load->view('login_view', $this->sign());
     }

     function validasi_login() {
          $this->form_validation->set_rules('username_user', 'Username', 'required|callback_username_login');
          $this->form_validation->set_rules('password_user', 'Password', 'required|callback_password_login');
          $this->form_validation->set_message('required', '%s harus diisi!!!');
          return $this->form_validation->run();
     }

     function username_login($username_post) {
          $this->load->model('register_model');
          $check = $this->register_model->check_unique_uname($username_post);
          if ($check == 0) {
               $this->form_validation->set_message('username_login', '%s tidak_terdaftar');
               return false;
          } else {
               return true;
          }
     }

          function password_login($password_post) {
          $this->load->model('register_model');
          $this->load->library('encrypt');
          $username = $this->input->post('username_user');
          $pass = $this->register_model->check_login($username)->row()->password_user;
          $password = $this->encrypt->decode($pass);
          if ($password != $password_post) {
               $this->form_validation->set_message('password_login', '%s tidak sesuai dengan password dari username yang anda masukkan');
               return false;
          } else {
               return true;
          }
     }

     public function login_validasi() {
          if ($this->validasi_login()) {
               $this->load->model('register_model');
               $this->load->library('session');
               $this->load->library('encrypt');
               $username = $this->input->post('username_user');
               $id_user = $this->register_model->check_login($username)->row()->id_user;
               $id_rule = $this->register_model->check_login($username)->row()->id_rule;
               $session = array(
                   'id_user' => $this->encrypt->encode($id_user),
                   'id_rule' => $this->encrypt->encode($id_rule)
               );
               $this->session->set_userdata($session);
               redirect('topik','refresh');
               
          } else {
               $this->login();
          }
     }

     public function sign_up() {
          $this->load->view('registrasi_view', $this->reg());
     }

     function validasi_sign_up() {
          $this->form_validation->set_rules('nama_user', 'Nama Lengkap', 'required');
          $this->form_validation->set_rules('username_user', 'Username', 'callback_username_check');
          $this->form_validation->set_rules('password_user', 'Password', 'required');
          $this->form_validation->set_rules('re_password_user', 'Konfirmasi Password', 'required|callback_password_check');
          $this->form_validation->set_rules('tempat_lahir_user', 'Tempat Lahir', 'required');
          $this->form_validation->set_rules('tanggal_lahir_user', 'Tanggal Lahir', 'required');
          $this->form_validation->set_rules('no_ktp_user', 'Nomor KTP', 'required');
          $this->form_validation->set_message('required', 'field %s harus diisi!!!');
          return $this->form_validation->run();
     }

     function username_check($srt) {
          $this->load->model('register_model');
          $username = $this->input->post('username_user');
          $check = $this->register_model->check_unique_uname($username);
          if ($username == null) {
               $this->form_validation->set_message('username_check', '%s harus diisi!!!');
               return false;
          } else if ($check > 0) {
               $this->form_validation->set_message('username_check', '%s sudah ada');
               return false;
          } else {
               return true;
          }
     }

     public function submit_sign_up() {

          if ($this->validasi_sign_up()) {
               //belum diberikan session
               $this->load->library('encrypt');
               $this->load->model('register_model');
               $data_register = array(
                   'nama_user' => $this->input->post('nama_user'),
                   'username_user' => $this->input->post('username_user'),
                   'password_user' => $this->encrypt->encode($this->input->post('password_user')),
                   'id_rule' => 1,
                   'tempat_lahir_user' => $this->input->post('tempat_lahir_user'),
                   'tanggal_lahir_user' => $this->input->post('tanggal_lahir_user'),
                   'alamat_user' => $this->input->post('alamat_user'),
                   'no_ktp_user' => $this->input->post('no_ktp_user'),
               );
               $this->register_model->submit_register($data_register);
          } else {
               $this->sign_up();
          }
     }

     public function logout(){
          $this->load->library('session');
          $this->session->sess_destroy();
          redirect('registrasi/login');
     }

}
